class Spirit {
  constructor(path) {
    this.img = utils.imageFromPath(path)
    this.x = 0
    this.y = 0
    this.w = 0
    this.h = 0

    this.loaded = false
    this.img.onload = () => {
      this.loaded = true
      this.w = this.img.width
      this.h = this.img.height
    }
  }

  static new(...args) {
    return new this(...args)
  }

  draw(context) {
    if (!context) {
      throw new ReferenceError('no context')
    }
    if (this.loaded) {
      context.drawImage(this.img, this.x, this.y, this.w, this.h)
    }
  }

  // 等待图片加载完成
  wait(fn) {
    setTimeout(() => {
      if (this.loaded) {
        fn()
      } else {
        this.wait(fn)
      }
    }, 10);
  }

  scale(amount = 1) {
    this.wait(() => {
      this.w *= amount
      this.h *= amount
    })
  }
}